{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6d35506e-bdae-4b9f-9140-6b2ae927f80e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d94f803e-d670-4a24-8df0-4bd7e62fcdd9",
   "metadata": {},
   "source": [
    "## 分箱\n",
    "\n",
    "### 什么是分箱操作等宽分箱?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "00bdcee0-7d63-4d50-8822-f76554b4386b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 30], (0, 30], (0, 30], (30, 60], (30, 60], (30, 60], (60, 100], (60, 100], (60, 100], (60, 100]]\n",
       "Categories (3, interval[int64, right]): [(0, 30] < (30, 60] < (60, 100]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n",
    "bins = [0, 30, 60, 100]\n",
    "labels = ['Low', 'Medium', 'High']\n",
    "pd.cut(data, bins, labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "41a94d51-08ec-4e4d-83ed-2669d9628b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.0, 30.0), [0.0, 30.0), [30.0, 60.0), [30.0, 60.0), [30.0, 60.0), [60.0, 100.1), [60.0, 100.1), [60.0, 100.1), [60.0, 100.1), [60.0, 100.1)]\n",
       "Categories (3, interval[float64, left]): [[0.0, 30.0) < [30.0, 60.0) < [60.0, 100.1)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pd.cut(data, bins, right=False)\n",
    "pd.cut(data, bins, labels=labels, right=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5911f6af-2b4b-435c-af01-a8350efea698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Low', 'Low', 'Medium', 'Medium', 'Medium', 'High', 'High', 'High', 'High', 'High']\n",
       "Categories (3, object): ['Low' < 'Medium' < 'High']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 问题：左闭右开导致NaN数据； 解决：扩大边界\n",
    "data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n",
    "bins = [0, 30, 60, 100 + 0.1]\n",
    "labels = ['Low', 'Medium', 'High']\n",
    "pd.cut(data, bins, labels=labels, right=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ebe6fed-9db9-4922-96e3-decadf38898e",
   "metadata": {},
   "source": [
    "### 什么是分箱操作等频分箱?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a0679478-338c-4058-aea0-e3a92e9c0775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Low', 'Low', 'Low', 'Medium', 'Medium', 'Medium', 'High', 'High', 'High', 'High']\n",
       "Categories (3, object): ['Low' < 'Medium' < 'High']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]\n",
    "# bins = [0, 30, 60, 100]\n",
    "labels = ['Low', 'Medium', 'High']\n",
    "quantiles = pd.qcut(data, q=3, labels=labels)\n",
    "display(quantiles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e38b2845-9a9b-412e-a4af-c243c09f5ad9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Grand</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>50</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>60</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>70</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>80</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>90</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>100</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Math   Grand\n",
       "0    10     Low\n",
       "1    20     Low\n",
       "2    30     Low\n",
       "3    40  Medium\n",
       "4    50  Medium\n",
       "5    60  Medium\n",
       "6    70    High\n",
       "7    80    High\n",
       "8    90    High\n",
       "9   100    High"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Math': data, 'Grand': quantiles})\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7a39a11-0890-4369-af2c-f54a9b948e65",
   "metadata": {},
   "source": [
    "##### 验证：等频分箱的频率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "63a34871-2b72-4ab8-b243-f74b33ba238f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Low', 'High', 'Low', 'Medium', 'High', ..., 'High', 'Low', 'Medium', 'Medium', 'Medium']\n",
       "Length: 50\n",
       "Categories (3, object): ['Low' < 'Medium' < 'High']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([  0.        ,  47.33333333,  85.66666667, 119.        ])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = np.random.randint(0, 120, size=50)\n",
    "labels = ['Low', 'Medium', 'High']\n",
    "quantiles, bins = pd.qcut(data, q=3, labels=labels, retbins=True)\n",
    "display(quantiles, bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "23f68be2-733b-4c64-b7ff-afa1c5f6731b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Low', 'High', 'Low', 'Medium', 'High', ..., 'High', 'Low', 'Medium', 'Medium', 'Medium']\n",
       "Length: 50\n",
       "Categories (3, object): ['Low' < 'Medium' < 'High']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(data, bins=bins, labels=labels)\n",
    "# display(quantiles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "17e39e25-62a0-4e92-a3d4-6fd180cb6066",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Math</th>\n",
       "      <th>Grand</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>117</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>46</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>85</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>109</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>27</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>113</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>42</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>24</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>13</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>8</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>26</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>116</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>74</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>60</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>35</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>87</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>103</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>103</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>4</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>90</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>50</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>86</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>75</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>93</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>89</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>86</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>53</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>73</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>100</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>56</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>3</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>35</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>96</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>101</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>63</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>119</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>74</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>32</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>15</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>51</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>75</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>53</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>26</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>119</td>\n",
       "      <td>High</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>3</td>\n",
       "      <td>Low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>59</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>76</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>54</td>\n",
       "      <td>Medium</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Math   Grand\n",
       "0      9     Low\n",
       "1    117    High\n",
       "2     46     Low\n",
       "3     85  Medium\n",
       "4    109    High\n",
       "5     27     Low\n",
       "6    113    High\n",
       "7     42     Low\n",
       "8     24     Low\n",
       "9     13     Low\n",
       "10     8     Low\n",
       "11    26     Low\n",
       "12   116    High\n",
       "13     0     NaN\n",
       "14    74  Medium\n",
       "15    60  Medium\n",
       "16    35     Low\n",
       "17    87    High\n",
       "18   103    High\n",
       "19   103    High\n",
       "20     4     Low\n",
       "21    90    High\n",
       "22    50  Medium\n",
       "23    86    High\n",
       "24    75  Medium\n",
       "25    93    High\n",
       "26    89    High\n",
       "27    86    High\n",
       "28    53  Medium\n",
       "29    73  Medium\n",
       "30   100    High\n",
       "31    56  Medium\n",
       "32     3     Low\n",
       "33    35     Low\n",
       "34    96    High\n",
       "35   101    High\n",
       "36    63  Medium\n",
       "37   119    High\n",
       "38    74  Medium\n",
       "39    32     Low\n",
       "40    15     Low\n",
       "41    51  Medium\n",
       "42    75  Medium\n",
       "43    53  Medium\n",
       "44    26     Low\n",
       "45   119    High\n",
       "46     3     Low\n",
       "47    59  Medium\n",
       "48    76  Medium\n",
       "49    54  Medium"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Grand\n",
       "High      17\n",
       "Low       16\n",
       "Medium    16\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Math': data, 'Grand': quantiles})\n",
    "display(df)\n",
    "\n",
    "df['Grand'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32ac8c45-7285-404c-86ed-6b39d1de1778",
   "metadata": {},
   "source": [
    "### 什么是分箱操作自定义分箱?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2b9e1a24-e04f-4086-b0a8-3a67cce1c1e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          Young\n",
       "1          Young\n",
       "2    Middle-aged\n",
       "3    Middle-aged\n",
       "4         Senior\n",
       "5         Senior\n",
       "6         Senior\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ages = [18, 25, 35, 50, 65, 80, 90]\n",
    "\n",
    "def custom_age_binning(age):\n",
    "    if age <= 30:\n",
    "        return 'Young'\n",
    "    elif age <= 60:\n",
    "        return 'Middle-aged'\n",
    "    else:\n",
    "        return 'Senior'\n",
    "\n",
    "age_series = pd.Series(ages)\n",
    "custom_bins = age_series.apply(custom_age_binning)\n",
    "\n",
    "display(custom_bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ff1c4583-88eb-4f21-892f-e038b7e6d5a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          Young\n",
       "1          Young\n",
       "2    Middle-aged\n",
       "3    Middle-aged\n",
       "4         Senior\n",
       "5         Senior\n",
       "6         Senior\n",
       "dtype: category\n",
       "Categories (3, object): ['Young' < 'Middle-aged' < 'Senior']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "s = pd.Series(pd.cut(ages, bins = [0, 30, 60, 91], right=True, labels=['Young', 'Middle-aged', 'Senior']))\n",
    "\n",
    "display(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6483968-1eae-4930-862b-08e8e6478064",
   "metadata": {},
   "source": [
    "#### 类别型数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "39bd79a3-8934-45dc-81c8-00c37d54fd46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    B\n",
       "1    A\n",
       "2    C\n",
       "3    A\n",
       "4    B\n",
       "dtype: category\n",
       "Categories (3, object): ['A' < 'B' < 'C']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = pd.Categorical(['B', 'A', 'C', 'A', 'B'], categories=['A', 'B', 'C'], ordered=True)\n",
    "s = pd.Series(data)\n",
    "display(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1b3700dc-f9f4-481b-8994-d5eb201a4871",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CategoricalDtype(categories=['A', 'B', 'C'], ordered=True, categories_dtype=object)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# int\n",
    "display(type(2))\n",
    "\n",
    "# float\n",
    "display(type(3.14))\n",
    "\n",
    "# str\n",
    "display(type('Hello'))\n",
    "\n",
    "# CategoricalDtype\n",
    "display(s.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a76bdb4b-1e9a-42d5-bac1-26f13660116d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    A\n",
       "3    A\n",
       "0    B\n",
       "4    B\n",
       "2    C\n",
       "dtype: category\n",
       "Categories (3, object): ['A' < 'B' < 'C']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(s.sort_values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5731ad9f-0907-485d-ab60-5693d613c548",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['B', 'A', 'C', 'A', 'B']\n",
       "Categories (3, object): ['A' < 'B' < 'C']"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 修改顺序\n",
    "data.set_categories(['C', 'B', 'A'])\n",
    "display(data)\n",
    "\n",
    "# s2 = pd.Series(data)\n",
    "# display(s2, 'sorted', s2.sort_values())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
